package data

import (
	"context"
	"gitee.com/jacktuotuo/coolcar/app/trip/internal/biz"
	mgo "gitee.com/jacktuotuo/coolcar/pkg/mongo"
	"github.com/go-kratos/kratos/v2/log"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
)

var _ biz.TripRepo = (*tripRepo)(nil)

type tripRepo struct {
	data     *Data
	log      *log.Helper
	tripColl *mongo.Collection
	newObjID func() primitive.ObjectID
}

type Location struct {
	Latitude  float64
	Longitude float64
}

type LocationStatus struct {
	Location *Location
	FeeCent  int32
	KmDriven float64
	PoiName  string
}

type TripRecord struct {
	mgo.IDField        `bson:"inline"`
	mgo.UpdatedAtField `bson:"inline"`
	AccountId          string
	CarId              string
	Start              *LocationStatus
	Current            *LocationStatus
	End                *LocationStatus
	Status             int32
	IdentityId         string
}

func (r *tripRepo) CreateTrip(ctx context.Context, t *biz.Trip) (*biz.Trip, error) {

	l := &Location{
		Latitude:  t.Start.Location.Latitude,
		Longitude: t.Start.Location.Longitude,
	}

	ls := &LocationStatus{
		Location: l,
		FeeCent:  0,
		KmDriven: 0,
		PoiName:  "研祥大厦",
	}

	tr := &TripRecord{
		AccountId:  t.AccountId,
		CarId:      t.CarId,
		Start:      ls,
		Current:    ls,
		Status:     int32(t.Status),
		IdentityId: "",
	}

	tr.ID = mgo.NewObjID()
	tr.UpdatedAt = mgo.UpdatedAt()

	_, err := r.tripColl.InsertOne(ctx, tr)
	if err != nil {
		return nil, err
	}

	t.Id = tr.ID.Hex()

	return t, nil
}

func (r *tripRepo) UpdateTrip(ctx context.Context, t *biz.Trip) (*biz.Trip, error) {
	return nil, nil
}
